package com.xukai.springboot.util.session;

import com.xukai.springboot.util.common.Common;
import com.xukai.springboot.util.encrypt.Encrypt;
import com.xukai.springboot.util.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import javax.annotation.PostConstruct;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * session处理类
 */
@Service
@Scope("prototype")
public class SessionService {

    @Autowired
    RedisService<Map<String, Object>> redisService;


    private int timeOut = 60 * 60 * 60;


    public String ss;

    public void inio() {
        ss = "asdfasdfsd";
    }


    @Autowired
    HttpServletRequest request;

    @Autowired
    HttpServletResponse response;

    /**
     * 获取Session
     *
     * @return
     */
    private String GetToken() {
        //ServletRequestAttributes requestAttributes = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes());
//        HttpServletRequest request = requestAttributes.getRequest();
//        HttpServletResponse response = requestAttributes.getResponse();
        //从参数中获取token
        String token = request.getHeader("token");

        if (token == null || token.isEmpty()) {
            token = UUID.randomUUID().toString();
            token = Encrypt.getInstance().MD5(token);
            response.addHeader("token", token);
            String dd = response.getHeader("token");

            return token;
        }
        return token;
    }

    /**
     * 添加vale
     *
     * @param key
     * @param obj
     */
    public void addValue(String key, Object obj) {
        String token = GetToken();
        String sessionKey = "session-" + token;
        ConcurrentHashMap<String, Object> session = (ConcurrentHashMap<String, Object>) redisService.get(sessionKey);
        if (session == null) {
            session = new ConcurrentHashMap<String, Object>();
        }
        session.put(key, obj);

        redisService.set(sessionKey, session, timeOut);
        redisService.refreshLiveTime(key, timeOut);
    }

    /**
     * 获取session信息
     *
     * @param key
     * @return
     */
    public Object getSessionByKey(String key) {
        String token = GetToken();
        String sessionKey = "session-" + token;
        if (!redisService.exists(sessionKey)) {
            return null;
        }
        ConcurrentHashMap<String, Object> session = (ConcurrentHashMap<String, Object>) redisService.get(sessionKey);

        if (!session.containsKey(key)) {
            return null;
        }
        redisService.refreshLiveTime(key, timeOut);
        return session.get(key);
    }

    /**
     * 设置当前用户
     *
     * @param currentUser
     */
    public void setCurrentUser(Object currentUser) {
        String token = GetToken();
        String sessionKey = "session-" + token;
        ConcurrentHashMap<String, Object> session = (ConcurrentHashMap<String, Object>) redisService.get(sessionKey);
        if (session == null) {
            session = new ConcurrentHashMap<String, Object>();
        }
        session.put("currentUser", currentUser);
        redisService.set(sessionKey, session, timeOut);
        redisService.refreshLiveTime(sessionKey, timeOut);
    }

    /**
     * 获取当前用户
     *
     * @return
     */
    public Object getCurrentUser() {
        String token = GetToken();
        String sessionKey = "session-" + token;
        if (!redisService.exists(sessionKey)) {
            return null;
        }
        ConcurrentHashMap<String, Object> session = (ConcurrentHashMap<String, Object>) redisService.get(sessionKey);
        if (!session.containsKey("currentUser")) {
            return null;
        }
        return session.get("currentUser");
    }

    /**
     * 验证token
     *
     * @param token
     * @return
     */
    public boolean checkToken() {
        String token = GetToken();
        String sessionKey = "session-" + token;
        if (redisService.exists(sessionKey)) {
            redisService.refreshLiveTime(sessionKey, timeOut);
            return true;
        }
        return false;
    }

    /**
     * 删除session
     *
     * @return
     */
    public boolean removeSession() {
        String token = GetToken();
        String sessionKey = "session-" + token;
        if (redisService.exists(sessionKey)) {
            redisService.delete(sessionKey);
            return true;
        }
        return false;
    }

}
